﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using YuFeng.Models.Product;
using YuFeng.Services.Product;
using YuFeng.Web.Filters;
using YuFeng.Web.Helpers;

namespace YuFeng.Web.Areas.Admin.Controllers
{
    [UserLoginFilter]
    public class ApplicationController : BaseController
    {
        private readonly IProductionService _productionService;
        public ApplicationController(IProductionService productionService)
        {
            _productionService = productionService;
        }
        public ActionResult IndustryApplication(string name, int page = 1)
        {
            var news = _productionService.GetPageProducts(name, (int)YuFeng.Models.AppEnum.ProductionType.IndustryApplication, false, page, 10);
            ViewBag.Name = name;
            return View(news);
        }
        public ActionResult Create()
        {
            Product product = new Product() { Type = (int)YuFeng.Models.AppEnum.ProductionType.IndustryApplication };
            return View(product);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(Product product)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _productionService.Create(product);
                    return this.ProcessFormatedResult(true, "创建成功！", "IndustryApplication");
                }
                else
                {
                    return this.ProcessFormatedResult(false, this.ExtendErrors(), model: product);
                }
            }
            catch (Exception e)
            {
                return this.ProcessFormatedResult(false, e.Message, model: product);
            }
        }
        public ActionResult Edit(string id)
        {
            try
            {
                var production = _productionService.GetProductByID(id);
                if (production == null)
                {
                    throw new Exception(string.Format("编号为{0}的应用不存在", id));
                }
                return View(production);
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Product product)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _productionService.Update(product);
                    return this.ProcessFormatedResult(true, "编辑成功！", "Edit", null, new { id = product.Id });
                }
                else
                {
                    return this.ProcessFormatedResult(false, this.ExtendErrors(), model: product);
                }
            }
            catch (Exception e)
            {
                return this.ProcessFormatedResult(false, e.Message, model: product);
            }
        }
        [HttpPost]
        public ActionResult DeleteProduction(string id)
        {
            try
            {
                var production = _productionService.GetProductByID(id);
                if (production == null)
                {
                    return ErrorMsg("应用不存在或已删除！");
                }
                else
                {
                    production.IsDeleted = true;
                    _productionService.Update(production);
                    return SuccessMsg("删除成功！");
                }
            }
            catch (Exception e)
            {
                return ErrorMsg(e.Message);
            }
        }
        public ActionResult Upload()
        {
            var result = FilesHelper.Upload(Utils.AppConst.ProductionImgFold);
            return Json(new
            {
                Entities = result
            });
        }
        public ActionResult UploadImgs()
        {
            var result = FilesHelper.Upload(Utils.AppConst.ProductionImgFold);
            return Json(new
            {
                link = result[0].PublicUrl
            });
        }
    }
}